package com.example.springboot.service.impl;

import com.example.springboot.Exception.ServiceException;
import com.example.springboot.common.Page;
import com.example.springboot.service.UserFavoriteProductService;
import com.example.springboot.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (UserFavoriteProduct)表服务实现类
 *
 * @author makejava
 * @since 2024-03-02 19:02:04
 */
@Service("userFavoriteProductService")

public class UserFavoriteProductServiceImpl implements UserFavoriteProductService {
    @Autowired
    private UserFavoriteProductMapper userFavoriteProductMapper;
    @Autowired
    private ProductServiceImpl productService;

    /**
     * 获取该商品的被收藏量
     *
     * @param productId
     * @return
     */
    @Override
    public BigDecimal getFavoriteCount(Integer productId) {
        try {
            int favoriteCount = userFavoriteProductMapper.getFavoriteCount(productId);
            return new BigDecimal(favoriteCount);
        } catch (Exception e) {
            throw new ServiceException("获取收藏量失败");
        }
    }

    @Override
    public Page<Product> loadUserFavorites(Integer currentPage, Integer pageSize, Integer id) {
        try {
            // 通过用户的ID查找商品id集合
            List<UserFavoriteProduct> userFavoriteProducts = userFavoriteProductMapper.loadUserFavorites(id);
            List<Integer> productIds = userFavoriteProducts.stream().map(userFavoriteProduct -> userFavoriteProduct.getProductId()).collect(Collectors.toList());
            // 通过商品id获取商品信息
            // 将productIds集合转为数组
            Integer[] productIdsArray = productIds.toArray(new Integer[productIds.size()]);

            List<Product> products = productService.selectByProductIds(currentPage, pageSize, productIdsArray);

            int total = productService.getUserFavoriteProductCount(productIdsArray);

            Page<Product> page = new Page<>();
            page.setList(products);
            page.setTotal(products.size());

            return page;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取收藏列表失败");
        }
    }

    @Override
    public Page<UserFavoriteProduct> getFavoritesByUserId(Integer currentPage, Integer pageSize, Integer id) {
        try {
            // 计算起始行
            int startRow = (currentPage - 1) * pageSize;
            int total = userFavoriteProductMapper.getFavoriteCountByUserId(id);
            List<UserFavoriteProduct> favorites = userFavoriteProductMapper.getFavoritesByUserId(startRow, pageSize, id);
            Page<UserFavoriteProduct> page = new Page<>();
            page.setTotal(total);
            page.setList(favorites);
            return page;
        } catch (Exception e) {
            throw new ServiceException("获取收藏列表失败");
        }
    }

    @Override
    public void deleteById(Integer id) {
        try {
            userFavoriteProductMapper.deleteById(id);
        } catch (Exception e) {
            throw new ServiceException("删除收藏失败");
        }
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                userFavoriteProductMapper.deleteById(id);
            }
        } catch (Exception e) {
            throw new ServiceException("删除收藏失败");
        }
    }

    @Override
    public UserFavoriteProduct selectByUserIdAndProductId(Integer userId, Integer productId) {
        try {
            return userFavoriteProductMapper.selectByUserIdAndProductId(userId, productId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("查询收藏失败");
        }
    }

    @Override
    public void add(Integer userId, Integer productId) {
        try {
            // 获取创建时间
            Date now = new Date();
            String createTime = DateUtil.getStringDate(now);
            // 创建收藏对象
            UserFavoriteProduct userFavoriteProduct = new UserFavoriteProduct();
            userFavoriteProduct.setUserId(userId);
            userFavoriteProduct.setProductId(productId);
            userFavoriteProduct.setCreateTime(now);
            userFavoriteProduct.setCreateTimeStr(createTime);
            userFavoriteProductMapper.add(userFavoriteProduct);

        } catch (Exception e) {
            throw new ServiceException("添加收藏失败");
        }
    }
}
